import { NodeEventType, Sprite, SpriteFrame, UITransform, _decorator, v3 } from "cc";
import BaseDialog from "./BaseDialog";
import SpriteFrameHelper from "../../helper/SpriteFrameHelper";
const {ccclass, property} = _decorator;
@ccclass
export default class ImageViewDialog extends BaseDialog{
    @property(Sprite)
    private img:Sprite=null
    protected onLoad(): void {
        //
    }
    protected onEnable(): void {
        //console.log('ImageViewDialog onEnable')
        //this.onSizeChanged()
        this.node.on(NodeEventType.SIZE_CHANGED,this.onSizeChanged,this)
        this.node.on('click',this.onBgClick,this)
        this.img.node.setPosition(v3(0,0,0))
    }
    private onBgClick():void{
        //console.log('ImageViewDialog.onBgClick')
        this.onClose()
    }
    protected onDisable(): void {
        this.node.off(NodeEventType.SIZE_CHANGED,this.onSizeChanged,this)
        this.node.off('click',this.onBgClick,this)
    }
    setSpriteFrame(sf:SpriteFrame|string):void{
        //this.img.node.setScale(v3(1,1,1))
        if(sf instanceof SpriteFrame){
            this.img.spriteFrame=sf
            this.onSizeChanged()
        }else{
            SpriteFrameHelper.getSpriteFrame(sf).then((sf)=>{
                this.img.spriteFrame=sf
                //this.onSizeChanged()
            })
        }
    }
    private onSizeChanged(){
        //console.log('ImageViewDialog onSizeChanged')
        const {width,height}=this.node.getComponent(UITransform)
        console.log(width,height)
        const uit=this.img.getComponent(UITransform)
        const scale=Math.min(width/uit.width,height/uit.height)
        this.img.node.setScale(v3(scale,scale,1))
    }
}